package com.eqdd.yiqidian.ui.companyspace

import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.support.v7.widget.LinearLayoutManager
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.DemandMatchBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.kotlin.imgPathPreview
import com.eqdd.yiqidian.utils.ImageUtil
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.util.*

/**
 * Created by jhj on 18-7-10.
 */
class DemandMatchProductActivity : BaseActivity() {

    var mvcHelper: MVCCoolHelper<List<DemandMatchBean>>? = null
    private var demandId: Int = -1

    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "匹配的产品")
        demandId = intent.getIntExtra(Config.ID, -1)
        if (demandId == -1) {
            toast("获取详情失败")
            finish()
            return
        }

    }

    override fun initData() {

        var pageNum = 0
        val list = arrayListOf<DemandMatchBean>()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        val slimAdapterEx = SlimAdapterEx.create().register<DemandMatchBean>(R.layout.list_item_153, object : SlimInjector<DemandMatchBean> {
            override fun onInject(data: DemandMatchBean?, injector: IViewInjector<out IViewInjector<*>>?) {

                val span = SpannableString(String.format(Locale.CHINA, "价格：￥%s", data?.productPrice))
                span.setSpan(ForegroundColorSpan(Color.RED), 3, span.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

                injector
                        ?.with<ImageView>(R.id.iv_image) { view ->
                            if (data?.image != null && "" != data.image) {
                                view.visibility = View.VISIBLE
                                view.onClick {
                                    imgPathPreview(data.image)
                                }
                                Glide.with(this@DemandMatchProductActivity)
                                        .asBitmap()
                                        .apply(ImageUtil.optionsNormal)
                                        .load(data.image)
                                        .into(object : SimpleTarget<Bitmap>() {
                                            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>) {
                                                view.background = BitmapDrawable(resources, resource)
                                            }
                                        })
                            } else {
                                view.visibility = View.GONE
                            }
                        }
                        ?.text(R.id.tv_name, String.format(Locale.CHINA, "产品名称：%s", data?.productName))
                        ?.text(R.id.tv_post, String.format(Locale.CHINA, "产品类型：%s", data?.productType))
                        ?.text(R.id.tv_msg, span)

            }
        }).attachTo(recyclerView).updateData(list as List<DemandMatchBean>?) as SlimAdapterEx<DemandMatchBean>?
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener { recyclerView, position, v ->
            slimAdapterEx?.getDataItem(position)?.let {
                startActivity<ProductInfoActivity>(Config.ID to it.id)
            }
        }


        val dataSource = ModelRx2DataSource<DemandMatchBean>(ModelRx2DataSource.OnLoadSource<DemandMatchBean> { page, register ->
            pageNum = if (page == 1) 0 else pageNum
            OkGo.post<HttpResult<List<DemandMatchBean>>>(HttpConfig.BASE_URL + HttpConfig.DEMAND_MATCH)
                    .params("demandId", demandId)
                    .params("page", pageNum)
                    .params("count", 10)
                    .converter(object : JsonConverter<HttpResult<List<DemandMatchBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    }).adapt(FlowableBody<HttpResult<List<DemandMatchBean>>>())
                    .flatMap({ result ->
                        if (page == 1) {
                            mvcHelper?.setAutoLoadMore(true)
                        }
                        pageNum = result.page
                        Flowable.just<MutableList<DemandMatchBean>>(if (result.items == null) ArrayList() else result.items as MutableList<DemandMatchBean>?)
                    })

        }, 10)
        mvcHelper = MVCCoolHelper<List<DemandMatchBean>>(coolRefreshView)
        mvcHelper?.setDataSource(dataSource)
        mvcHelper?.adapter = slimAdapterEx
        mvcHelper?.refresh()
    }

    override fun setView() {

    }
}